public class LinearSearch {

    private LinearSearch() {};

    /**
     * 如果查询到目标元素，则返回目标元素所在的索引；查询不到，返回-1
     * @param data 元数据数组
     * @param target 查询目标元素
     * @return
     */
    public static <E> int search(E[] data, E target) {
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals(target)) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] dataSize = { 1000000, 10000000 };
        for (int n : dataSize) {
            Integer[] data = ArrayGenerator.generateOrderedArray(n);
            // System.nanoTime() 获取到当前时间戳，单位是纳秒
            long startTime = System.nanoTime();
            // 循环执行100次线性查找算法
            for (int k = 0; k < 100; k++)
                LinearSearch.search(data, n);
            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("n = " + n + ", 100 runs : " + time + "s");
        }
    }


   /* public static void main(String[] args) {

        Integer[] data = {24, 18, 12, 9, 16, 66, 32, 4};

        int res = LinearSearch.search(data, 16);
        System.out.println(res);

        int res2 = LinearSearch.search(data, 999);
        System.out.println(res2);

        Student[] students = {new Student("zzm"), new Student("zhangsan"), new Student("lisi")};
        Student zzm = new Student("zzm");
        int res3 = LinearSearch.search(students, zzm);
        System.out.println(res3);
    }*/
}
